package com.jjmmbb.anydolist;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ToggleButton;

public class StrikeActivity extends BaseActivity implements OnClickListener {
	Button btSave, btCancel;
	// ToggleButton bt1, bt2, bt3, bt4, bt5, bt6;
	// ToggleButton bt7, bt8, bt9, bt10, bt11, bt12;
	// ToggleButton bt13, bt14, bt15, bt16, bt17, bt18;
	// ToggleButton bt19, bt20, bt21, bt22, bt23, bt24;

	ToggleButton[] arrBtn = new ToggleButton[25];

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.strike);
		arrBtn[1] = (ToggleButton) findViewById(R.id.strike_bt1);
		arrBtn[2] = (ToggleButton) findViewById(R.id.strike_bt2);
		arrBtn[3] = (ToggleButton) findViewById(R.id.strike_bt3);
		arrBtn[4] = (ToggleButton) findViewById(R.id.strike_bt4);
		arrBtn[5] = (ToggleButton) findViewById(R.id.strike_bt5);
		arrBtn[6] = (ToggleButton) findViewById(R.id.strike_bt6);
		arrBtn[7] = (ToggleButton) findViewById(R.id.strike_bt7);
		arrBtn[8] = (ToggleButton) findViewById(R.id.strike_bt8);
		arrBtn[9] = (ToggleButton) findViewById(R.id.strike_bt9);
		arrBtn[10] = (ToggleButton) findViewById(R.id.strike_bt10);
		arrBtn[11] = (ToggleButton) findViewById(R.id.strike_bt11);
		arrBtn[12] = (ToggleButton) findViewById(R.id.strike_bt12);
		arrBtn[13] = (ToggleButton) findViewById(R.id.strike_bt13);
		arrBtn[14] = (ToggleButton) findViewById(R.id.strike_bt14);
		arrBtn[15] = (ToggleButton) findViewById(R.id.strike_bt15);
		arrBtn[16] = (ToggleButton) findViewById(R.id.strike_bt16);
		arrBtn[17] = (ToggleButton) findViewById(R.id.strike_bt17);
		arrBtn[18] = (ToggleButton) findViewById(R.id.strike_bt18);
		arrBtn[19] = (ToggleButton) findViewById(R.id.strike_bt19);
		arrBtn[20] = (ToggleButton) findViewById(R.id.strike_bt20);
		arrBtn[21] = (ToggleButton) findViewById(R.id.strike_bt21);
		arrBtn[22] = (ToggleButton) findViewById(R.id.strike_bt22);
		arrBtn[23] = (ToggleButton) findViewById(R.id.strike_bt23);
		arrBtn[24] = (ToggleButton) findViewById(R.id.strike_bt24);

		btSave = (Button) findViewById(R.id.strike_save);
		btCancel = (Button) findViewById(R.id.strike_cancel);

		btSave.setOnClickListener(this);
		btCancel.setOnClickListener(this);

		ini();
	}

	void ini() {
		int strike = Util.getPreferenceStrike(this);
		System.out.println("ini,strike:" + strike);
		ArrayList<Integer> list = Util.MarkDecodeByBinary(strike);
		for (int i : list) {
			arrBtn[i].setChecked(true);
		}
	}

	private void saveToPre() {
		ArrayList<Integer> list = new ArrayList<Integer>();
		for (int i = 1; i < 25; i++) {
			if (arrBtn[i].isChecked()) {
				list.add(i);
			}
		}
		int strike = Util.MarkCodeByBinary(list);
		System.out.println("save,strike:" + strike);

		Util.saveStrike2Preferences(this, strike);
		this.finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.strike_save:
			saveToPre();
			break;
		case R.id.strike_cancel:
			this.finish();
			break;
		default:
			break;
		}

	}

}
